package com.boot.security.server.controller.mffl.MfflService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.boot.security.server.model.mffl.Vo.MfflServiceVo;
import com.boot.security.server.service.impl.mffl.MfflService.MfflServiceImpl;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.boot.security.server.page.table.PageTableRequest;
import com.boot.security.server.page.table.PageTableHandler;
import com.boot.security.server.page.table.PageTableResponse;
import com.boot.security.server.page.table.PageTableHandler.CountHandler;
import com.boot.security.server.page.table.PageTableHandler.ListHandler;
import com.boot.security.server.dao.mffl.MfflServiceDao;
import com.boot.security.server.model.mffl.MfflService;

import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

@Api(tags = "服务")
@RestController
@RequestMapping("/mfflServices")
public class MfflServiceController {

    @Autowired
    private MfflServiceDao mfflServiceDao;

    @Autowired
    private MfflServiceImpl mfflserviceimpl;

    @PostMapping
    @ApiOperation(value = "保存")
    @PreAuthorize("hasAnyAuthority('sys:service:add')")
    public MfflService save(@RequestBody MfflService mfflService) {
        mfflServiceDao.save(mfflService);
        return mfflService;
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据id获取")
    @PreAuthorize("hasAnyAuthority('sys:service:query')")
    public MfflServiceVo get(@PathVariable Long id) {
        return mfflServiceDao.getById(id);
    }

    @PutMapping
    @ApiOperation(value = "修改")
    @PreAuthorize("hasAnyAuthority('sys:service:update')")
    public MfflService update(@RequestBody MfflServiceVo mfflService) {
        mfflserviceimpl.update(mfflService);
        return mfflService;
    }

    @GetMapping
    @ApiOperation(value = "列表")
    @PreAuthorize("hasAnyAuthority('sys:service:query')")
    public PageTableResponse list(PageTableRequest request) {
        Map<String, Object> params = request.getParams();
        params.put("delete_state",1);
        params.put("toconfigure",1);
        request.setParams(params);

        return new PageTableHandler(new CountHandler() {
            @Override
            public int count(PageTableRequest request) {
                return mfflServiceDao.count(request.getParams());
            }
        }, new ListHandler() {

            @Override
            public List<MfflService> list(PageTableRequest request) {
                return mfflServiceDao.list(request.getParams(), request.getOffset(), request.getLimit());
            }
        }).handle(request);
    }

    @ApiIgnore
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除")
    @PreAuthorize("hasAnyAuthority('sys:MfflLawfirm:del')")
    public void delete(@PathVariable Long id) {
        mfflServiceDao.delete(id);
    }
}
